@auth.requires_login()
def check_session_address():
    '''
    Checks for any addresss created by the current user; if none, redirect to create address form.
    Checks for valid address in session, else prompts to reselect
    >>> addresss=db(db.address.created_by=="1").select().as_list()
    >>> addresss
    [{'phone': '480-834-5627', 'address': '2323 W. 14th Street Tempe, AZ 85281', 'id': 1, 'name': 'KUNC', 'url': 'http://kunc.org', 'created_on': '2010-01-24 23:28:15', 'created_by': '1', 'phone_long_distance': '1-800-888-8888'}]
    '''
    addresss = db(db.address.created_by==auth.user.id).select().as_list()
    valid = False
    if len(addresss) < 1:
        user_message = T('May we have your mailing address for our records?')
        redirect(URL(r=request, f='create_address',vars=dict(user_message=user_message)))
    elif (not request.function=='session_address_id_form' and not session.address):
        redirect(URL(r=request, f='session_address_id_form'))
    elif not request.function=='session_address_id_form':
        for rec in addresss:
            if rec['id'] == session.address_id:
                valid = True
        if not valid:
            redirect(URL(r=request, f='session_address_id_form'))

@auth.requires_login()
def view_address():
    check_session()
    address_id=request.args(0)#only without call to get_valid_id(request.args(0),db.table), since validated below 
    # address_id=session.address['id']
    addresss=db(db.address.created_by==auth.user.id).select(orderby=db.address.address)
    address=db.address[address_id] or redirect(error_page)
    if address in addresss:
        return dict(address=address)
    else:
        redirect(error_page)

@auth.requires_login()
def list_addresses():
    # check_session()
    addresss=db(db.address.created_by==auth.user.id).select(orderby=db.address.address)
    return dict(addresss=addresss)

@auth.requires_login()
def edit_address():
    check_session()
    address_id=session.address['id']
    address=db.address[address_id] or redirect(error_page)
    form=crud.update(db.address,address,next=url('list_addresses'))
    return dict(form=form)

@auth.requires_login()
def create_address():
    # check_session()
    # form=crud.create(db.address,next=url('index'))
    # form = SQLFORM(db.address,next=url('index'))

    form = SQLFORM(db.address)
    if no_address_message != 'no message':
        message=no_address_message

    if form.accepts(request.vars, session): 
        response.flash='record inserted'

        address_id = dict(form.vars)['id']
        address = db(db.address.id==address_id).select()

        session.address = address.as_list()[0]
        session.address_id = address.as_list()[0]['id']

        redirect(URL(r=request, f='index'))

    elif form.errors: response.flash='form errors'
    return dict(form=form,message=message)

@auth.requires_login()
def check_session():
    check_session_address()
#    check_session_pledgedrive()
#    check_session_segment()

@auth.requires_login()
def index():
    display_authenticated_user_menus()
    check_session()   # equivalent to 
    """
    example action using the internationalization operator T and flash
    rendered by views/default/index.html or views/generic.html
    """

    organization = session.organization
    pledgedrive=session.pledgedrive
    program=session.program
    person=session.person
    challenge=session.challenge
    segment=session.segment

#    return dict()
    return dict(pledgedrive=pledgedrive,person=person,challenge=challenge,segment=segment)
#    return dict(pledgedrive=pledgedrive,person=person,challenge=challenge,segment=segment)

@auth.requires_login()
def session_address_id_form():
    addresss=db(db.address.created_by==auth.user.id).select(orderby=db.address.address)    
    ids=[o.id for o in addresss]
    names=[o.address for o in addresss]

    form=SQLFORM.factory(Field('address_id',requires=IS_IN_SET(ids,names))) 
    
    if form.accepts(request.vars, session):
        address_id = form.vars.address_id
        address = db(db.address.id==address_id).select()
        session.address = address.as_list()[0]
        session.address_id = address.as_list()[0]['id']
        redirect(URL(r=request, f='index'))

    response.flash = T('Add or select an address to make it the current working address.')
    return dict(form=form,addresss=addresss)
